﻿///////////////////////////////////////////////////////////////
// Projet serveur entreprise
// Authors: Pierre Corsini, Guillaume Blanc
// mars 2011
///////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLL;
using System.Collections;

namespace InterfaceClient
{
    public partial class Login : System.Web.UI.Page
    {
        ArrayList comptes;
        Utilisateur current;
        CoucheMetier dll;
        protected void contelist_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadAccount();
        }
        protected void Page_Load(object sender, EventArgs e)
        {


            if (true.Equals(Session["isConnected"]))
            {
                whenconnected.Visible = true;
                whennotconnected.Visible = false;

                current = Session["user"] as Utilisateur;
                Securite.seConnecter(current.User, current.Pwd, Securite.Droit.Client);
                dll = new CoucheMetier();
                comptes = new ArrayList();

                if (!this.IsPostBack)
                    loadAccounts();
            }
            else
            {
                whenconnected.Visible = false;
                whennotconnected.Visible = true;
            }
        }
        protected void seconnecter_Click(object sender, EventArgs e)
        {
            Utilisateur found = Securite.seConnecter(user.Text, pwd.Text, Securite.Droit.Client);

            if (found != null)
            {
                Session["isConnected"] = true;
                Session["dll"] = new CoucheMetier();
                Session["user"] = found;
                Response.Redirect(Request.RawUrl);
            }
            else
            {
                errorlabel.Text = "Mauvais identifiants !";
            }
        }
        private void loadAccount()
        {
            historiquelist0.Items.Clear();
            foreach (DLL.Historique h in dll.listeHistorique(Int32.Parse(comptelist.SelectedValue)))
                historiquelist0.Items.Add(h.ToString());
        }
        private void loadAccounts()
        {
            comptelist.Items.Clear();
            foreach (DLL.Compte c in dll.listeCompte())
            {
                comptelist.Items.Add(new ListItem(c.ToString(), c.Id.ToString()));
            }
        }
    }
}